import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.util.Iterator;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.Border;

public class MainFrame extends JFrame
{	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1594129301530177813L;
	private SelectionPanel first, second;
	private JPanel facultyPanel;
	private JPanel buttonPanel;
	private ChartPanel chartPanel;
	private String[] colleges = {""};
	private String[] faculties = {""};
	private JButton go;
	protected View window = null;
	
	JMenuItem quit;
	JMenuItem reset;
	JMenuItem console;
	
	ProxyWindow proxy;
	DefaultComboBoxModel firstFacultyModel;
	DefaultComboBoxModel secondFacultyModel;
	DefaultComboBoxModel firstCollegeModel;
	DefaultComboBoxModel secondCollegeModel;
	
	public MainFrame(View view)
	{
		super("University Research Ranker");
		window = view;
		initGUI();
	}
	
	private void initGUI() 
	{
		try {
			BorderLayout thisLayout = new BorderLayout();
			getContentPane().setLayout(thisLayout);
			this.setSize(600, 775);
	
			setFirst(new SelectionPanel("First Institution", this.colleges));
			getFirst().setPreferredSize(getPreferredSize());
			setSecond(new SelectionPanel("Second Institution", this.colleges));
			getSecond().setPreferredSize(getPreferredSize());
			facultyPanel = new JPanel();
			facultyPanel.add(getFirst(), BorderLayout.NORTH);
			facultyPanel.add(getSecond(), BorderLayout.SOUTH);
			getContentPane().add(facultyPanel, BorderLayout.NORTH);
			facultyPanel.setPreferredSize(new java.awt.Dimension(592, 163));

			buttonPanel = new JPanel();
			buttonPanel.add(createButtonPanel(), BorderLayout.EAST);
			getContentPane().add(buttonPanel, BorderLayout.CENTER);
			buttonPanel.setPreferredSize(new java.awt.Dimension(592, 40));

			JPanel results = new JPanel();
			Border etched = BorderFactory.createEtchedBorder();
			Border titled = BorderFactory.createTitledBorder(etched, "Results");
			results.setBorder(titled);
			getContentPane().add(results, BorderLayout.SOUTH);
			results.setPreferredSize(new java.awt.Dimension(592, 520));
			{
				chartPanel = new ChartPanel();
				results.add(chartPanel);
				chartPanel.setPreferredSize(new java.awt.Dimension(500, 448));
			}
			
			JMenu menu = new JMenu("File");
	        menu.setMnemonic(KeyEvent.VK_F);
	        JMenuItem item = null;
	        //add new faculty
	        item = new JMenuItem("Add new faculty...");
	        item.setMnemonic(KeyEvent.VK_A);
	        item.addActionListener(new ActionListener() {
	            public void actionPerformed(ActionEvent e) 
	            {
					//JOptionPane.showMessageDialog(this,"Add Faculty");
	                System.out.println("Add Faculty");
	                window.cf.reset();
	                window.cf.setVisible(true);
	                window.cf.setAlwaysOnTop(true);
	            }
	        });
	        menu.add(item);
	        //set proxy
	        item = new JMenuItem("Set proxy...");
	        item.setMnemonic(KeyEvent.VK_P);
	        item.addActionListener(new ActionListener() {
	            public void actionPerformed(ActionEvent e) 
	            {
					proxy = new ProxyWindow();
	                proxy.setProxy.addActionListener(new ActionListener()
	                {
	                	public void actionPerformed(ActionEvent e) 
	                	{
	                		System.getProperties().put( "proxySet", "true" );
	                		System.getProperties().put( "proxyHost", proxy.host.getText() );
	                		System.getProperties().put( "proxyPort", proxy.port.getText() );
	                		proxy.dispose();
	                	}
	                });
	                
	                proxy.cancel.addActionListener(new ActionListener()
	                {
	                	public void actionPerformed(ActionEvent e) 
	                	{
	                		proxy.dispose();
	                	}
	                });
	                
	            }
	        });
	        menu.add(item);
	        
	        //new
	        reset = new JMenuItem("Reset");
	        reset.setMnemonic(KeyEvent.VK_N);
	        menu.add(reset);

	        //quit
	        quit = new JMenuItem("Quit");
	        quit.setMnemonic(KeyEvent.VK_Q);
	        
	        menu.add(quit);
	        
	        JMenu menu2 = new JMenu("View");
	        menu.setMnemonic(KeyEvent.VK_V);
	        //Show console window
	        console = new JMenuItem("Show Console");
	        console.setMnemonic(KeyEvent.VK_C);
	        menu2.add(console);

	        JMenuBar menuBar = new JMenuBar();
	        menuBar.add(menu);
	        menuBar.add(menu2);
	        setJMenuBar(menuBar);

			setDefaultCloseOperation(EXIT_ON_CLOSE);
			setVisible(true);

			setVisible(true);
		} 
		catch(Exception e) 
		{
			e.printStackTrace();
		}
	}
	
	public boolean checkInputs()
	{
		if(getFirst().getSelectedCollege().equals("Select College"))
		{
			JOptionPane.showMessageDialog(this,"Please choose the first college");
			return false;
		}
		if(getFirst().getSelectedFaculty().equals("Select Faculty"))
		{
			JOptionPane.showMessageDialog(this,"Please choose the first faculty");
			return false;
		}
		if(getSecond().getSelectedCollege().equals("Select College"))
		{
			JOptionPane.showMessageDialog(this,"Please choose the second college");
			return false;
		}
		if(getSecond().getSelectedFaculty().equals("Select Faculty"))
		{
			JOptionPane.showMessageDialog(this,"Please choose the second faculty");
			return false;
		}
		if(getFirst().getSelectedCollege().equals(getSecond().getSelectedCollege()) && getFirst().getSelectedFaculty().equals(getSecond().getSelectedFaculty()))
		{
			JOptionPane.showMessageDialog(this,"Please choose two different faculties to compare");
			return false;
		}
		return true;
	}
	
	public void setColleges(String[] newColleges)
	{
		this.colleges = new String[newColleges.length];
		this.colleges = newColleges;
	}

	/**
	 * 
	 */
	public void updateDropdowns() 
	{
		firstCollegeModel = new DefaultComboBoxModel (this.colleges);
		secondCollegeModel = new DefaultComboBoxModel (this.colleges);
		firstFacultyModel = new DefaultComboBoxModel(new String[]{"Select Faculty"});
		secondFacultyModel = new DefaultComboBoxModel(new String[]{"Select Faculty"});
		
		getFirst().collegeBox.setModel(firstCollegeModel);
		getFirst().facultyBox.setModel(firstFacultyModel);
		getSecond().collegeBox.setModel(secondCollegeModel);
		getSecond().facultyBox.setModel(secondFacultyModel);
	}
	
	public void updateFacultyDropdowns()
	{
	
		String [] faculties;
		String college;
		
		if(getFirst().collegePicked == true)
		{				
			college = getFirst().getSelectedCollege();
			Iterator <College> iter = window.getModel().colleges.iterator();
			while(iter.hasNext())
			{
				College c = iter.next();
				if(c.getCollegeName().equals(college))
				{
					faculties = c.returnFaculties();
					firstFacultyModel = new DefaultComboBoxModel(faculties);
					getFirst().facultyBox.setModel(firstFacultyModel);
				}
			}
		}
		
		if(getSecond().collegePicked == true)
		{
			college = getSecond().getSelectedCollege();
			Iterator <College> iter = window.getModel().colleges.iterator();
			while(iter.hasNext())
			{
				College c = iter.next();
				if(c.getCollegeName().equals(college))
				{
					faculties = c.returnFaculties();
					secondFacultyModel = new DefaultComboBoxModel(faculties);
					getSecond().facultyBox.setModel(secondFacultyModel);
					break;
				}
			}
		}
	}
	
	public JPanel createButtonPanel()
	{
		JPanel buttonPanel = new JPanel();
		setGo(new JButton("Go!"));
		//go.addQueryListener(this);
		getGo().setActionCommand("go");
		buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.X_AXIS));
		buttonPanel.add(getGo());
		return buttonPanel;
	}
	
	public void setGo(JButton go) 
	{
		this.go = go;
	}

	public JButton getGo() 
	{
		return go;
	}

	public static void main (String [] args)
	{
		MainFrame mf = new MainFrame(new View(new Model()));
	}

	/**
	 * @return
	 */
	public ChartPanel getChart() {
		// TODO Auto-generated method stub
		return chartPanel;
	}

	public void setFirst(SelectionPanel first) {
		this.first = first;
	}

	public SelectionPanel getFirst() {
		return first;
	}

	public void setSecond(SelectionPanel second) {
		this.second = second;
	}

	public SelectionPanel getSecond() {
		return second;
	}
}